﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace Morse
{
    public partial class Light : UserControl
    {
        Brush black = new SolidColorBrush(Colors.Black);
        Brush white = new SolidColorBrush(Colors.White);
        public static int Speed = 3;

        private bool stop = false;
        public Light()
        {
            InitializeComponent();
        }
        public void Play(string code)
        {
            stop = false;
            int Dit = MorseCode.Dit, Dah = MorseCode.Dah;
            ThreadPool.QueueUserWorkItem(_ =>
            {
                var steps = MorseEncode.GetPlaySteps(code);
                foreach (var step in steps)
                {
                    if (stop)
                        break;
                    this.Dispatcher.BeginInvoke(() => LayoutRoot.Background = step.Play ? white : black);
                    Thread.Sleep(step.Time * 3);
                }
            });
        }

        public void Stop()
        {
            stop = true;
        }
    }
}
